<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2007/03/26 15:12:50 $)
// $RCSfile: winapp_misc.php,v $ : $Revision: 1.12 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

if ($eventaction == "blockvisitor")
{
	if (trim($_POST["visitorsessionid"]) != "")
	{
		require_once ("./modules/livesupport/functions_livesupport.php");

		$_visitor = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."sessions` WHERE `sessionid` = '". $dbCore->escape($_REQUEST["visitorsessionid"]) ."';");
		if (trim($_visitor["ipaddress"]) != "")
		{
			addVisitorNote($_visitor["ipaddress"], sprintf($_SWIFT["language"]["visitorblocked"], $_SWIFT["staff"]["fullname"]));
		}

		blockVisitor($_POST["visitorsessionid"], $_SWIFT["staff"]["staffid"]);
	}

	echoWinappData("1");
} else if ($eventaction == "staffaddnote") {
	if (trim($_REQUEST["note"]) != "")
	{
		$_visitor = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."sessions` WHERE sessionid = '". $dbCore->escape($_REQUEST["visitorsessionid"]) ."';");
		if (trim($_visitor["ipaddress"]) != "")
		{
			require_once ("./modules/livesupport/functions_livesupport.php");
			addVisitorNote($_visitor["ipaddress"], urldecode($_REQUEST["note"]));
		}
	}

	echoWinappData("1");
} else if ($eventaction == "visitornotes") {
	if (trim($_REQUEST["visitorsessionid"]) != "")
	{
		// Get the IP
		$_visitor = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."sessions` WHERE `sessionid` = '". $dbCore->escape($_GET["visitorsessionid"]) ."';");

		if (trim($_visitor["ipaddress"]) != "")
		{
			$notecount = 0;
			$notes = array();

			$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."visitornotes` AS vn LEFT JOIN `". TABLE_PREFIX ."visitornotedata` AS vnd ON (vn.visitornoteid = vnd.visitornoteid) WHERE vn.ipaddress = '". $dbCore->escape($_visitor["ipaddress"]) ."';");
			while ($dbCore->nextRecord())
			{
				$notes[$notecount]["createdon"] = edate($_SWIFT["settings"]["dt_datetimeformat"], $dbCore->Record["dateline"]);
				$notes[$notecount]["createdby"] = $dbCore->Record["staffname"];
				$notes[$notecount]["staffid"] = $dbCore->Record["staffid"];
				$notes[$notecount]["contents"] = nl2br(htmlspecialchars($dbCore->Record["contents"]));

				$notecount++;
			}

			if ($notecount > 0)
			{
				$template->assign("hasnotes", "true");
				$template->assign("visitornotes", $notes);
			}

		}
	}
	echo $template->displayTemplate("winapptabnotes.tpl");


/**
* ###############################################
* CHAT STATUS
* ###############################################
*/
} else if ($eventaction == "visitorchatstatus") {
	if (trim($_REQUEST["visitorsessionid"]) != "")
	{
		$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."chatobjects` AS chatobjects LEFT JOIN `". TABLE_PREFIX ."chatdata` AS chatdata ON (chatobjects.chatobjectid = chatdata.chatobjectid) WHERE chatobjects.visitorsessionid = '". $dbCore->escape($_REQUEST["visitorsessionid"]) ."';");
		$convo = array();

		while ($dbCore->nextRecord())
		{
			$convo = array_merge($convo, unserialize($dbCore->Record["contents"]));
		}

		if (count($convo) > 0)
		{
			foreach ($convo as $key=>$val)
			{
				if ($val["base64"] == true)
				{
					$convo[$key]["message"] = base64_decode($val["message"]);
				}
			}
			$template->assign("ischatactive", "true");
			$template->assign("conversation", $convo);
		}
	}

	echo $template->displayTemplate("winapptabchatstatus.tpl");



/**
* ###############################################
* VISITOR HISTORY
* ###############################################
*/
} else if ($eventaction == "visitorhistory") {
	if (trim($_REQUEST["visitorsessionid"]) != "")
	{
		$template->assign("winappsessionid", $_SWIFT["session"]["sessionid"]);

		// Get the IP
		$_visitor = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."sessions` WHERE `sessionid` = '". $dbCore->escape($_GET["visitorsessionid"]) ."';");

		if (trim($_visitor["ipaddress"]) != "")
		{
			$historycount = 0;
			$history = array();

			$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."chatobjects` WHERE `ipaddress` = '". $dbCore->escape($_visitor["ipaddress"]) ."';");
			while ($dbCore->nextRecord())
			{
				$history[$historycount]["date"] = edate($_SWIFT["settings"]["dt_datetimeformat"], $dbCore->Record["dateline"]);
				$history[$historycount]["chatobjectid"] = $dbCore->Record["chatobjectid"];
				$history[$historycount]["userfullname"] = $dbCore->Record["userfullname"];
				$history[$historycount]["staffname"] = iif($dbCore->Record["chatstatus"] != CHAT_INCOMING, $dbCore->Record["staffname"], "");
				$history[$historycount]["departmenttitle"] = $dbCore->Record["departmenttitle"];

				$historycount++;
			}

			if ($historycount > 0)
			{
				$template->assign("hashistory", true);
				$template->assign("visitorhistory", $history);
			}
		}

		echo $template->displayTemplate("winapptabhistory.tpl");
	} else if (!empty($_GET["chatobjectid"])) {
		$template->assign("ishistorytab", true);

		$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."chatobjects` AS chatobjects LEFT JOIN `". TABLE_PREFIX ."chatdata` AS chatdata ON (chatobjects.chatobjectid = chatdata.chatobjectid) WHERE chatobjects.chatobjectid = '". intval($_GET["chatobjectid"]) ."';");
		$dbCore->nextRecord();
		$convo = array();
		$convo = unserialize($dbCore->Record["contents"]);
		foreach ($convo as $key=>$val)
		{
			$convo[$key]["message"] = str_replace("&amp;#", "&#", htmlspecialchars(iif($val["base64"]==true, base64_decode($val["message"]), $val["message"])));
		}

		$template->assign("ischatactive", "true");
		$template->assign("conversation", $convo);

		echo $template->displayTemplate("winapptabchatstatus.tpl");
	}
}

?>